package com.itheima.service.Impl;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public PageResult fingPage(QueryPageBean page) {

        if (page.getQueryString() == null) {
            int begin = (page.getCurrentPage() - 1) * page.getPageSize();
            List<CheckItem> list = checkItemMapper.findPage(begin, page.getPageSize());

            long selectCount = (long) checkItemMapper.selectCount(null);

            return new PageResult(selectCount, list);
        }


        try {
            int i = Integer.parseInt(page.getQueryString());
            System.out.println(i);
            int begin = (page.getCurrentPage() - 1) * page.getPageSize();
            List<CheckItem> list = checkItemMapper.queryPageByCode(begin, page.getPageSize(),
                    "%" + page.getQueryString() + "%");
            long count = checkItemMapper.countCode("%" + page.getQueryString() + "%");
            return new PageResult(count, list);
        } catch (NumberFormatException e) {
            int begin = (page.getCurrentPage() - 1) * page.getPageSize();
            List<CheckItem> list = checkItemMapper.queryPageByName(begin, page.getPageSize(),
                    "%" + page.getQueryString() + "%");
            long count = checkItemMapper.countName("%" + page.getQueryString() + "%");
            return new PageResult(count, list);
        }
    }

    @Override
    public Result add(CheckItem checkItem) {
        if (checkItem == null) {
            return new Result(false, "参数非法");
        }
        int count = checkItemMapper.insert(checkItem);
        if (count <= 0) {
            return new Result(false, "添加失败");
        }
        return new Result(true, "新增检查项成功");
    }

    @Override
    public Result deleteById(String id) {
        int count = checkItemMapper.deleteById(id);
        if (count <= 0) {
            return new Result(false, "删除失败");
        }
        return new Result(true, "删除检查项成功");
    }

    @Override
    public Result findById(Integer id) {
        CheckItem checkItem = checkItemMapper.selectById(id);
        return new Result(true, "查询检查项成功", checkItem);
    }

    @Override
    public Result update(CheckItem checkItem) {
        if (checkItem == null) {
            return new Result(false, "参数非法");
        }
        int count = checkItemMapper.updateById(checkItem);
        if (count<=0){
            return new Result(false, "修改失败");
        }
        return new Result(true,"编辑检查项成功");
    }

    @Override
    public Result findAll() {
        List<CheckItem> checkItemList =checkItemMapper.selectAll();
        return new Result(true,"查询检查项成功",checkItemList);
    }
}
